package org.midy.slh;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

public class SerializableMain {
    public static void main(String[] args) {
        String fileName = "c:\\A\\Object";
//        try {
//            File newFile = new File(fileName);
//            File parentFile = newFile.getParentFile();
//            if(!parentFile.exists()){
//                System.out.println("parentFile.mkdirs():" + parentFile.mkdirs());
//            }
//            if(!newFile.exists()) {
//                System.out.println("newFile.createNewFile():" + newFile.createNewFile());
//            } else {
//                newFile.delete();
//                newFile.createNewFile();
//            }
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//
//
//
//        try (
//                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//                ObjectOutputStream objectOutputStream = new ObjectOutputStream(Files.newOutputStream(Paths.get(fileName)));
//                ObjectOutputStream objectOutputStream2 = new ObjectOutputStream(byteArrayOutputStream);
//        ) {
//            KeepAliveCommand keepAliveCommand = new KeepAliveCommand();
//            keepAliveCommand.setAge(18);
//            keepAliveCommand.setTime("midy");
//            keepAliveCommand.setName("米大洋");
//            objectOutputStream2.writeObject(keepAliveCommand);
//            objectOutputStream.writeObject(keepAliveCommand);
//            objectOutputStream.flush();
//            objectOutputStream2.flush();
//            byte[] byteArray = byteArrayOutputStream.toByteArray();
//            System.out.println("byteArray.length:" + byteArray.length);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }

        try (
                ObjectInputStream objectInputStream = new ObjectInputStream(Files.newInputStream(Paths.get(fileName)))
        ) {
            KeepAliveCommand o = (KeepAliveCommand) objectInputStream.readObject();
            System.out.println(o.getAge());
            System.out.println(o.getTime());
//            System.out.println(o.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
